package com.yuncheng.oaApi.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.yuncheng.annotation.AutoLog;
import com.yuncheng.oaApi.busService.QyzxyybService;

import com.yuncheng.exception.BusinessException;
import com.yuncheng.oaApi.constant.OaFlowConstant;
import com.yuncheng.oaApi.constant.OaFlowNodeConstant;
import com.yuncheng.oaApi.entity.OaProcessBusiness;
import com.yuncheng.oaApi.entity.QyReservation;
import com.yuncheng.oaApi.service.IOaProcessBusinessService;
import com.yuncheng.oaApi.service.IQyReservationService;
import com.yuncheng.vo.HttpResult;
import com.yuncheng.oaApi.vo.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.context.annotation.Lazy;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.security.NoSuchAlgorithmException;
import java.security.spec.InvalidKeySpecException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Api(tags = "NEW_OA-企业咨询填写")
@RestController
@RequestMapping("/api/spzx-oa/qyzxtx")
public class QyyyzxController {
    @Resource
    @Lazy
    private QyzxyybService qyzxyybService;


    @Resource
    @groovy.lang.Lazy
    private IOaProcessBusinessService oaProcessBusinessService;


    @Resource
    @groovy.lang.Lazy
    private IQyReservationService qyReservationService;

    @AutoLog(value = "发送验证码")
    @ApiOperation(value = "发送验证码")
    @GetMapping(value = "fsyzm")
    public HttpResult<?> fsyzm(String fsj) throws InvalidKeySpecException, InvocationTargetException, NoSuchAlgorithmException, IllegalAccessException, IOException {
        String fsyzm = qyzxyybService.fsyzm(fsj);
        return HttpResult.ok(fsyzm);
    }



    @AutoLog(value = "预约登记")
    @ApiOperation(value = "预约登记")
    @GetMapping(value = "yydj")
    public HttpResult<?> yydj(String fsj,String fyzm) throws InvocationTargetException, IllegalAccessException {
        Map yydj = qyzxyybService.yydj(fsj, fyzm);
//        Map yydj = qyzxyybService.yydj(fsj);
        return HttpResult.ok(yydj);

    }

    @AutoLog(value = "刷新")
    @ApiOperation(value = "刷新")
    @GetMapping(value = "sxyyb")
    public HttpResult<?> sxyyb(String yybId) {
        Map sxyyb = qyzxyybService.sxyyb(yybId);
        return HttpResult.ok(sxyyb);
    }


    @AutoLog(value = "更新业务主表")
    @ApiOperation(value = "更新业务主表")
    @PostMapping(value = "updateZb")
    public HttpResult<?> updateZb(@RequestBody  HashMap<String,String> yyb) throws InvocationTargetException, IllegalAccessException {
        Result result = qyzxyybService.updateZb(yyb);
        return HttpResult.ok(result.getMessage());

    }



    @AutoLog(value = "更新子表")
    @ApiOperation(value = "更新子表")
    @GetMapping(value = "updateJb")
    public HttpResult<?> updateJb(String id,String fxzzt){
        Result result = qyzxyybService.updateJb(id, fxzzt);
        return HttpResult.ok(result.getMessage());
    }

    @AutoLog(value = "提交")
    @ApiOperation(value = "提交")
    @GetMapping(value = "tjNr")
    public HttpResult<?> tjNr(String yybId){
        Result result = qyzxyybService.tjNr(yybId);
        return HttpResult.ok(result);
    }

    @AutoLog(value = "退回内容")
    @ApiOperation(value = "退回内容")
    @GetMapping(value = "thNr")
    public HttpResult<?> thNr(String yybId){
        Result result = qyzxyybService.thNr(yybId);
        return HttpResult.ok(result.getMessage());
    }



    @AutoLog(value = "验证类别")
    @ApiOperation(value = "验证类别")
    @GetMapping(value = "yzLb")
    public HttpResult<?> yzLb(String yybId){
        Result result = qyzxyybService.yzLb(yybId);
        if (result.isSuccess()){
            return HttpResult.ok(result.getMessage());
        }else {
            throw new BusinessException("没有选择咨询事项类别");
        }
    }


    @AutoLog(value = "已咨询预约列表")
    @ApiOperation(value = "已咨询预约列表")
    @GetMapping(value = "zxyyList")
    public HttpResult<?> zxyyList(String fsj,String yzm){
        List list = qyzxyybService.zxyyList(fsj,yzm);
        return HttpResult.ok(list);
    }



    @AutoLog(value = "获取预约日期")
    @ApiOperation(value = "获取预约日期")
    @GetMapping(value = "yyrq")
    public HttpResult<?> yyrq(){
        List yyrq = qyzxyybService.yyrq();
        return HttpResult.ok(yyrq);
    }



    /**
     * 手机端退回
     */
    @AutoLog(value = "手机端企业退回")
    @ApiOperation(value = "手机端企业退回")
    @GetMapping(value = "sjdth")
    public HttpResult<?> sjdList(String id,String yzm,String opinion) {
        QyReservation qyReservation = qyReservationService.getById(id);
        String flczt = qyReservation.getFlczt();
        if (StringUtils.isNotBlank(flczt) && flczt.equals(OaFlowNodeConstant.HJMC_LCJS)){
            throw new BusinessException("当前流程已结束不支持撤回");
        }
        if (StringUtils.isBlank(yzm)) {
            throw new BusinessException("验证码为空");
        }

        if (!yzm.equals(qyReservation.getFyzm())) {
                throw new BusinessException("验证码不正确");

        }else {

            //更新这条数据的验证码
            qyReservation.setFyzm(yzm);
            qyReservation.setFsfcxyy(OaFlowConstant.YES_QYCJ_WBL);
            qyReservation.setFqycxyy(opinion);
            qyReservationService.updateById(qyReservation);

            List<OaProcessBusiness> processBusinesses = oaProcessBusinessService.list(new LambdaQueryWrapper<OaProcessBusiness>()
                    .eq(OaProcessBusiness::getFpid, id));
            if (CollectionUtils.isNotEmpty(processBusinesses)) {
                OaProcessBusiness oaProcessBusiness = processBusinesses.get(0);
                oaProcessBusiness.setFqysfcx(OaFlowConstant.YES_QYCJ_WBL);
                oaProcessBusinessService.updateById(oaProcessBusiness);
            }
        }

        if (!yzm.equals(qyReservation.getFyzm())){
            //验证不成功
            qyReservation.setFqycxyy("");
            qyReservation.setFsfcxyy(OaFlowConstant.YES_QYCJ_WBL);
            qyReservationService.updateById(qyReservation);


            List<OaProcessBusiness> processBusinesses = oaProcessBusinessService.list(new LambdaQueryWrapper<OaProcessBusiness>()
                    .eq(OaProcessBusiness::getFpid, id));
            if (CollectionUtils.isNotEmpty(processBusinesses)) {
                OaProcessBusiness oaProcessBusiness = processBusinesses.get(0);
                oaProcessBusiness.setFqysfcx(OaFlowConstant.YES_QYCJ_WBL);
                oaProcessBusinessService.updateById(oaProcessBusiness);
            }

            throw new BusinessException("验证码不正确，请核对验证码");
        }
        return HttpResult.ok("撤回成功");
    }






}
